﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using System.Web.UI.WebControls;
using Cognitum.WebPages.Bll;

namespace Cognitum.WebPages.Account
{
    public partial class UserProfile : System.Web.UI.Page
    {
        private ProfileBase profile;

        //CloudBlobClient blobClient = null;
        //CloudBlobContainer blobContainer = null;

        private void ShowControls(bool isCurrentUser)
        {
            PrivateGender.Visible = isCurrentUser;
            PublicGender.Visible = !isCurrentUser;
            UploadPortrait.Visible = isCurrentUser;
            SubmitButton.Visible = isCurrentUser;
        }

        private ProfileBase GetProfile(out bool isCurrentUser)
        {
            var requestedUserName = Request.Params["name"];
            var currentUser = Membership.GetUser();

            ProfileBase foundProfile = null;

            if (currentUser != null && (requestedUserName == currentUser.UserName || String.IsNullOrEmpty(requestedUserName)))
            {
                foundProfile = HttpContext.Current.Profile;
                isCurrentUser = true;
            }
            else
            {
                isCurrentUser = false;

                if (String.IsNullOrEmpty(requestedUserName))
                {
                    // Nothing to do since no user was specified
                    FormsAuthentication.RedirectToLoginPage();
                    return foundProfile; ;
                }

                foundProfile = ProfileBase.Create(requestedUserName, true);
            }

            if (foundProfile == null)
            {
                // Profile was not found
                FormsAuthentication.RedirectToLoginPage();
            }

            return foundProfile;
        }

        private string GetGender()
        {
            string gender;
            switch ((int)profile.GetPropertyValue("Gender"))
            {
                case -1:
                    gender = Resources.Profile.NotSet;
                    break;

                case 0:
                    gender = Resources.Profile.GenderFemale;
                    break;

                case 1:
                    gender = Resources.Profile.GenderMale;
                    break;

                default:
                    throw new ArgumentOutOfRangeException(Resources.Profile.UnknownGenderError);
            }
            return gender;
        }

        private void PopulateGenderDropDown()
        {
            var genderVal = (int)profile.GetPropertyValue("Gender");
            PrivateGender.Items.Add(
                new ListItem(Resources.Profile.NotSet, "-1") { Selected = (genderVal == -1) }
                );
            PrivateGender.Items.Add(
                new ListItem(Resources.Profile.GenderFemale, "0") { Selected = (genderVal == 0) }
                );
            PrivateGender.Items.Add(
                new ListItem(Resources.Profile.GenderMale, "1") { Selected = (genderVal == 1) }
                );
        }

        private string GetPortrait()
        {
            return (string)profile.GetPropertyValue("PortraitBlobAddressUri");
        }

        private void InitControls(bool isCurrentUser)
        {
            ShowControls(isCurrentUser);

            TitleUserName.Text = UserName.Text = profile.UserName;

            if (isCurrentUser)
            {
                PopulateGenderDropDown();
            }
            else
            {
                PublicGender.Text = GetGender();
            }
            PortraitLabel.Text = GetPortrait();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            bool isCurrentUser;

            profile = GetProfile(out isCurrentUser);
            if (profile == null) return;

            if (!IsPostBack) InitControls(isCurrentUser);
        }

        protected void SubmitButton_Click(Object sender, EventArgs eventArgs)
        {
            var genderVal = Int32.Parse(PrivateGender.SelectedValue);
            profile.SetPropertyValue("Gender", genderVal);
        }

        protected void PortraitSubmitButton_Click(object sender, EventArgs e)
        {
            if (profile == null || String.IsNullOrEmpty(PortraitUploadControl.FileName)) return;

            profile.SetPropertyValue("PortraitBlobAddressUri", String.Format("Portrait of {0}", Membership.GetUser().UserName));

            var extension = System.IO.Path.GetExtension(PortraitUploadControl.FileName);

            var blobAddressUri = String.Format("{0}{1}", Guid.NewGuid(), extension);

            profile.SetPropertyValue("PortraitBlobAddressUri", blobAddressUri);

            var userName = Membership.GetUser().UserName;

            GetPortrait();
        }
    }
}